package user;

import book.Book;
import book.Library;
import utils.ScannerSingleton;

import java.sql.SQLOutput;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Scanner;

public class AdminUser extends User{

    public Scanner scanner;

    public Library library;



    public AdminUser(String name, int userID) {
        super(name, userID);
        scanner = ScannerSingleton.getScannerSingleton();
        library = Library.getLibrary();
    }

    @Override
    public int display() {
        System.out.println(this.getName() + "的操作菜单：");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.上架图书");
        System.out.println("5.修改图书");
        System.out.println("6.下架图书");
        System.out.println("7.统计借阅次数");
        System.out.println("8.查看最受欢迎的前N本书");
        System.out.println("9.查看库存");
        System.out.println("10.检查上架超过一年的图书");
        return scanner.nextInt();
    }

    //上架图书
    public void addBook(){
        System.out.println("管理员类的addBook方法执行了");
        scanner.nextLine();

        System.out.println("请输入书名：");
        String title = scanner.nextLine();

        System.out.println("请输入作者：");
        String author = scanner.nextLine();

        System.out.println("请输入出版年份：");
        int publishYear = scanner.nextInt();

        LocalDateTime shelfDate = LocalDateTime.now();

        Book book = new Book(title,author,publishYear,shelfDate);

        library.addBook(book);

    }

    //修改图书
    public void updateBook() {
        System.out.println("管理员类的updateBook方法执行了");
        //1.打印目前所有的书籍
        library.displayBook();

        System.out.println("输入你要修改图书的ISBN");
        int ISBN = scanner.nextInt();
        scanner.nextLine();


        Book book = library.searchById(ISBN);

        if (book == null) {
            System.out.println("没有找到你输入ISBN的书籍");
            return;
        }


        System.out.println("当前书名： " + book.getTitle());
        System.out.println("输入新的书名： ");
        String newTitle = scanner.nextLine();

        scanner.nextLine();

        System.out.println("当前作者： " + book.getAuthor());
        System.out.println("输入新的作者： ");
        String newAuthor = scanner.nextLine();

        scanner.nextLine();

        System.out.println("当前出版时间： " + book.getTitle());
        System.out.println("输入新的出版时间： ");
        int newPublishYear = scanner.nextInt();

        book.setTitle(newTitle);
        book.setAuthor(newAuthor);
        book.setPublishYear(newPublishYear);

        library.updateBook(book);


    }

    //下架图书
    public void removeBook() {
        System.out.println("管理员类的removeBook方法执行了");
        library.displayBook();

        System.out.println("输入要删除书籍的ISBN码：");
        int ISBN = scanner.nextInt();
        scanner.nextLine();

        Book removeBook = library.searchById(ISBN);
        if (removeBook == null) {
            System.out.println("没有你要删除的书籍");
            return;
        }

        library.removeBook(ISBN);
        System.out.println("图书" + removeBook.getTitle() + "已被删除");

    }

    //查看图书的借阅次数
    public void borrowCount() {
        System.out.println("管理员类的borrowCount方法执行了");
        library.borrowCount();

    }

    //查看最受欢迎的前N本书
    public void generateBook() {
        System.out.println("管理员类的generateBook方法执行了");
        System.out.println("查看最受欢迎的前N本书不能超过" + library.bookCount + "本");
        System.out.println("输入你要查看前几本最受欢迎的书籍");
        int n = scanner.nextInt();
        scanner.nextLine();
        if (n <= 0 || n > library.bookCount) {
            System.out.println("没有找到最受欢迎的前N本书！");
            return;
        }
        library.generateBook(n);

    }

    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("管理员类的checkInventoryStatus方法执行了");
        library.checkInventoryStatus();

    }

    //移除上架超过1年的书籍
    public void checkRemoveOldBooks() {
        System.out.println("管理员类的checkRemoveOldBooks方法执行了");
        library.checkRemoveOldBooks();

    }


}
